package com.dykj.fireFighting.web.base.news;

import java.util.*;

import com.dykj.fireFighting.api.base.news.dto.NoticeNewsDto;
import com.dykj.fireFighting.api.base.news.entity.NoticeNews;
import com.dykj.fireFighting.api.base.news.service.INoticeNewsService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 消息通知（3.2版本新增）接口
 *
 * @Author JacksonTu
 * @Date  2021-08-24 15:19:34
 */
@Slf4j
@Api(value = "消息通知（3.2版本新增）接口", tags = {"消息通知（3.2版本新增）接口"})
@RestController
@RequestMapping("news/noticeNews")
public class NoticeNewsController {

    @DubboReference
    private INoticeNewsService noticeNewsService;

    /**
     * 获取单位id
     *
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     *
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }


    /**
     * 消息通知（3.2版本新增）列表
     *
     * @return
     */
    @AutoLog(value = "消息通知（3.2版本新增）-分页列表查询")
    @ApiOperation(value = "消息通知（3.2版本新增）分页列表", notes = "消息通知（3.2版本新增）分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<NoticeNews>> treeGrid(NoticeNewsDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<NoticeNews> page = noticeNewsService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取消息通知（3.2版本新增）信息
     */
    @AutoLog(value = "消息通知（3.2版本新增）-获取消息通知（3.2版本新增）信息")
    @ApiOperation(value = "获取消息通知（3.2版本新增）信息", notes = "获取消息通知（3.2版本新增）信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<NoticeNews> info(@PathVariable("id") String id){
        NoticeNews noticeNews = noticeNewsService.getById(id);
        return CommonResult.success(noticeNews);
    }

    /**
     * 保存消息通知（3.2版本新增）信息
     */
    @AutoLog(value = "消息通知（3.2版本新增）-保存消息通知（3.2版本新增）信息")
    @ApiOperation(value = "保存消息通知（3.2版本新增）信息", notes = "保存消息通知（3.2版本新增）信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody NoticeNews noticeNews) {
        try {
            noticeNews.setCreateTime(new Date());
            noticeNews.setUnitId(getEnterpriseId());
            noticeNews.setCreateBy(getName());
                noticeNewsService.save(noticeNews);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改消息通知（3.2版本新增）信息
     */
    @AutoLog(value = "消息通知（3.2版本新增）-修改消息通知（3.2版本新增）信息")
    @ApiOperation(value = "修改消息通知（3.2版本新增）信息", notes = "修改消息通知（3.2版本新增）信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody NoticeNews noticeNews) {
        try {
            noticeNews.setUpdateTime(new Date());
            noticeNews.setUpdateBy(getName());
                noticeNewsService.updateById(noticeNews);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除消息通知（3.2版本新增）信息
     */
    @AutoLog(value = "消息通知（3.2版本新增）-删除消息通知（3.2版本新增）信息")
    @ApiOperation(value = "删除消息通知（3.2版本新增）信息", notes = "删除消息通知（3.2版本新增）信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                noticeNewsService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
